<?php
/**
* Model Class
* 
* @author Guillermo Landín
* @link 
*/ 

class Adminusermodel extends Model {

    function __construct()
    {
        parent::Model();
        
        $this->CI =& get_instance(); 
    }

    /**
    * Check if backoffice user exists
    *
    * @access public
    * @param string
    * @return boolean
    */ 
    function check_user($username,$password,$fields="*")
    {
        $this->db->select($fields)   
                ->from('admin_users')
                ->where('Username', $username)
                ->where('Password', $password)
                ->where('Active', 1);

        $query = $this->db->get();
        return $query->row_array();
    }
    
    public function get_all_users($fields="*")
    {
        $this->db->select($fields)   
                ->from('admin_users')
                ->join('admin_roles', 'admin_users.Role = admin_roles.id_Roles')
                ->order_by('Role, Username');

        $query = $this->db->get();
        return $query->result_array();
    }
     
    public function get_all_roles($fields="*") 
    {                                                                                           
        $this->db->select($fields)
                ->from('admin_roles')
                ->order_by('id_Roles');

        $query = $this->db->get();
        return $query->result_array();                                                          
    }
    public function get_user($id=NULL, $fields="*") 
    {
        $this->db->select($fields)
                ->from('admin_users')
                ->join('admin_roles', 'admin_users.Role = admin_roles.id_Roles')
                ->where('id', $id);

        $query = $this->db->get();
        $result = $query->row_array();          
        return empty($result) ? FALSE : $result;   
    }

    public function create_user($data=array())
    {
        $data["Active"] = 1;
        $this->db->insert('admin_users', $data);
        return $this->db->insert_id();
    } 
    public function update_user($id=FALSE, $data=array())
    {
        $this->db->where('id', $id)
                ->update('admin_users', $data); 

    } 
    public function delete_user($id=FALSE)
    {
        $this->db->where('id', $id);
        $this->db->delete('admin_users'); 

    }  

    public function is_duplicated($field, $value, $id=FALSE)
    {
    
        $this->db->select('count(1) as counter')
                ->from('admin_users')
                ->where($field, $value);        
        if($id!==FALSE) $this->db->where('id !=', $id);
    
        $query = $this->db->get();
        $row = $query->row_array();
        if($row == NULL)
            return FALSE;
        
        if($row["counter"] > 0){
            return TRUE;
        }else{
            return FALSE;
        }

    }  

}
    
    
/* End of file newsmodel.php */
/* Location: ./system/application/models/newsmodel.php */ 
